package com.thx.tags;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;
import com.thx.common.spring.BeanFactory;
import com.thx.common.web.WebConstant;
import com.thx.privilege.PriviManager;
import com.thx.privilege.PriviManagerFactory;
import com.thx.resource.model.Resource;
import com.thx.resource.service.ResourceManager;
import com.thx.role.model.Role;
import com.thx.role.service.RoleManager;
import com.thx.user.model.User;
import com.thx.user.service.UserManager;

/**
 * select标签的类.
 *
 * @author shiyan
 */
public class Privileged extends Component {

	private static final String PRIVITYPE_MENU = "sys_menu";

	private static final String IDNAME_CODE = "code";
	private static final String IDNAME_URL = "url";

	private HttpServletRequest request;

	private String id;

	private String idName;

	private String priviType;

	public Privileged(ValueStack stack, HttpServletRequest req) {
		super(stack);
		request = req;
	}

	@Override
	public boolean start(Writer writer) {
		boolean answer = false;
		User user = (User) request.getSession().getAttribute(WebConstant.KEY_USER);

		if (user == null) {
			return answer;
		}

		PriviManager priviManager = PriviManagerFactory.getpPriviManager();
		if (StringUtils.isBlank(priviType)) {
			priviType = PRIVITYPE_MENU;
		}
		if (StringUtils.isBlank(idName)) {
			idName = IDNAME_CODE;
		}
		if (PRIVITYPE_MENU.equals(priviType)) {
			String objId = "";
			ResourceManager resourceManager = (ResourceManager) BeanFactory.getBean("resourceManager");
			if (IDNAME_CODE.equals(idName)) {
				Resource resource = resourceManager.findByCode(id);
				if (resource != null) {
					objId = resource.getResourceId();
				}
			} else {
				Resource resource = resourceManager.findByUrl(id);
				if (resource != null) {
					objId = resource.getResourceId();
				}
			}
			answer = priviManager.hasUserPrivi(user.getUserId(), objId, PRIVITYPE_MENU);
			if (answer) {
				return answer;
			}
			user = ((UserManager) BeanFactory.getBean("userManager")).get(user.getUserId());

			List<Role> roleList = user.getRoleList();
			if (roleList == null) {
				roleList = ((RoleManager) BeanFactory.getBean("roleManager")).findByUserId(user.getUserId());
				if (roleList == null)
					roleList = new ArrayList<Role>();
				user.setRoleList(roleList);
			}

			Iterator<Role> it = roleList.iterator();
			while (it.hasNext() && !answer) {
				Role role = it.next();
				answer = priviManager.hasRolePrivi(role.getRoleId(), objId, PRIVITYPE_MENU);
			}
		}
		return answer;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIdName() {
		return idName;
	}

	public void setIdName(String idName) {
		this.idName = idName;
	}

	public String getPriviType() {
		return priviType;
	}

	public void setPriviType(String priviType) {
		this.priviType = priviType;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

}
